//
//  ByMineSettingVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/23.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByMineSettingVC.h"

// vc
#import "ByMineSettingAuthVC.h"     // 实名认证
#import "ByAddressManageVC.h"       // 地址选择
#import "BySetPayPasswardSetp1VC.h" // 设置交易密码
// cell
#import "ByCellSettingNormal.h"     // 默认cell

@interface ByMineSettingVC () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

@property (nonatomic, strong) UITableView *table;

@end

@implementation ByMineSettingVC
{
    ByIconChooseDataSource *_source;
    ZKBombView *_sexPicker;
    
    // cell里的一些属性和视图
    UIImageView *_iconView;     // 头像
    UITextField *_nickTf;       // 昵称
    NSString *_sex;             // 性别
    NSString *_authStatus;      // 认证状态
}

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"个人设置";

    _authStatus = @"您还未实名认证";
    
    [self addLeftBackBarButtonItem];
    
    [self createTableView];
}

#pragma mark - 创建视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsZero).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 3;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 选择头像
    if (indexPath.section == 0) {
        ByCellSettingNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"iconCell"];
        if (!cell) {
            cell = [[ByCellSettingNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"iconCell"];
            cell.title = @"我的头像";
            cell.subTitle = nil;
            _iconView = [UIImageView new];
            _iconView.layer.cornerRadius = 22;
            _iconView.layer.masksToBounds = true;
            _iconView.layer.borderWidth = 1;
            _iconView.layer.borderColor = BY_GOLD_COLOR.CGColor;
            [cell.contentView addSubview:_iconView];
            [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(44, 44));
                make.right.equalTo(cell).offset(-30);
                make.centerY.equalTo(cell);
            }];
        }
        _iconView.backgroundColor = [UIColor redColor];
        
        return cell;
    }
    //
    else if (indexPath.section == 1) {
        // 昵称
        if (indexPath.row == 0) {
            ByCellSettingNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"nickCell"];
            if (!cell) {
                cell = [[ByCellSettingNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"nickCell"];
                cell.title = @"昵称";
                cell.subTitle = nil;
                _nickTf = [UITextField new];
                _nickTf.delegate = self;
                _nickTf.text = USER.nickname;
                _nickTf.textColor = [UIColor blackColor];
                _nickTf.textAlignment = NSTextAlignmentRight;
                [cell.contentView addSubview:_nickTf];
                [_nickTf mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(cell);
                    make.left.equalTo(cell.titleLabel.mas_right).offset(10);
                    make.right.equalTo(cell).offset(-30);
                    make.centerY.equalTo(cell);
                }];
            }
            
            return cell;
        }
        // 性别
        if (indexPath.row == 1) {
            ByCellSettingNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"sexCell"];
            if (!cell) {
                cell = [[ByCellSettingNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"sexCell"];
                cell.title = @"性别";
                cell.subTitleLabel.textColor = [UIColor blackColor];
            }
            cell.subTitle = _sex == nil ? @"" : _sex;
            
            
            return cell;
        }
        // 收货地址
        if (indexPath.row == 2) {
            ByCellSettingNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"addressCell"];
            if (!cell) {
                cell = [[ByCellSettingNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"addressCell"];
                cell.title = @"收货地址";
            }
            
            return cell;
        }
    }
    // 交易密码
    else if (indexPath.section == 2) {
        
        ByCellSettingNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"payPwCell"];
        if (!cell) {
            cell = [[ByCellSettingNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"payPwCell"];
            cell.title = @"交易密码";
            
        }
        
        return cell;
    }
    // 实名认证
    else if (indexPath.section == 3) {
        
        ByCellSettingNormal *cell = [tableView dequeueReusableCellWithIdentifier:@"authCell"];
        if (!cell) {
            cell = [[ByCellSettingNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"authCell"];
            cell.titleLabel.textColor = BY_RED_COLOR;
            cell.subTitle = @"实名认证将获得大量积分奖励!";
        }
        cell.title = _authStatus;
        
        return cell;
    }
    
    return [UITableViewCell new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60;
    }
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    
    // 选择头像
    if (indexPath.section == 0) {
        [self selectIcon];
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [_nickTf becomeFirstResponder];
        }
        // 性别
        if (indexPath.row == 1) {
            [self selectSex];
        }
        // 收货地址
        else if (indexPath.row == 2) {
            [self addressManage];
        }
    }
    // 交易密码
    else if (indexPath.section == 2) {
        [self setPayPw];
    }
    // 实名认证
    else if (indexPath.section == 3) {
        [self auth];
    }
}

#pragma mark - 点击事件
// 选择头像
- (void)selectIcon {
    
    _source = [[ByIconChooseDataSource alloc] init];
    [_source fireWithViewController:self];
    [_source makeConfigToSelectIcon];
    __weak typeof(self) wself = self;
    [_source setCompleteBlock:^(UIImage *image) {
        typeof(self) ssefl = wself;
        UIImageView *iconView = ssefl->_iconView;
        iconView.image = image;
        [ByCoreToolAlert showOnlyText:@"头像选择完毕，请求上传"];
    }];
}
// 选择性别
- (void)selectSex {
    
    _sexPicker = [[ZKBombView alloc] init];
    _sexPicker.contentMode = ZKBombContentModePicker;
    _sexPicker.dataItems = @[@"男", @"女"];
    __weak typeof(self) wself = self;
    _sexPicker.pickerDidSelected = ^(NSString *text) {
        typeof(self) ssefl = wself;
        ssefl->_sex = text;
        [ssefl.table reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
        [ByCoreToolAlert showOnlyText:@"性别选择完毕，请求上传"];
    };
    [_sexPicker show];
}
// 实名认证
- (void)auth {
    
    ByMineSettingAuthVC *vc = [ByMineSettingAuthVC new];
    [self by_normalPush:vc hasNavi:true];
}
// 地址管理
- (void)addressManage {
    
    ByAddressManageVC *vc = [ByAddressManageVC new];
    [self by_normalPush:vc hasNavi:true];
}
// 设置交易密码
- (void)setPayPw {
    
    BySetPayPasswardSetp1VC *vc = [BySetPayPasswardSetp1VC new];
    [self by_normalPush:vc hasNavi:true];
}

#pragma mark - textField delegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    ByModelUser *user = USER;
    user.nickname = textField.text;
    [ByCommonCache setUserInfo:user];

    [ByCoreToolAlert showOnlyText:@"昵称填写完毕，请求上传"];
}


@end
